let vm = new Vue({
    el:'.main-container',  //选中整个main
    data() {
        return {
            page:{},
            isShow: false,
            name:'',
            condition:{
                dScope:'',
                oid:'',
                name:'',
                remarks:''
            },
            // highLight:false,
            setting: {
                data: {
                    simpleData: {
                        enable: true,
                        pIdKey: "parentId",//父节点的名字
                        idKey: "id" //子节点的名字
                    },//开启简单树
                    key: {
                        title: 'ztree'
                    }
                },
                callback: {//回掉函数
                    onClick: this.onClick
                },
                view: {
                    fontCss: this.fontCss
                }
            },
            nodes: [

            ]
        }
    },
    methods:{
        selectPage:function (start=1,pageSize=6) {
            this.page={};
            axios({
                url:`/guguanjia/manager/role/selectPage/${start}/${pageSize}`,   //相对路径同样受到base标签影响
                params:this.condition
            }).then(
                response=>{  //省略function关键字  自动传递上下文中的this
                    //this.apps = response.data.obj.list;
                    this.page = response.data.obj
                }
            ).catch(error=>{
                console.log(error.message);
            })
        },
        selectAllRole:function () {
            this.page={};
            axios({
                url:`/guguanjia/manager/role/getAllRole`,   //相对路径同样受到base标签影响
            }).then(
                response=>{  //省略function关键字  自动传递上下文中的this
                    //this.apps = response.data.obj.list;
                    this.roles = response.data.obj;
                }
            ).catch(error=>{
                console.log(error.message);
            })
        },
        init: function () {
            axios({
                url:"/guguanjia/manager/sysOffice" //相对路径同样受到base标签影响
            }).then(
                response=>{  //省略function关键字  自动传递上下文中的this
                    //this.apps = response.data.obj.list;
                    this.nodes = response.data.obj
                    this.nodes[this.nodes.length] = {'id':0,'name':'全部机构'};
                    $.fn.zTree.init($("#pullDownTreeone"), this.setting, this.nodes);
                }
            ).catch(error=>{
                console.log(error.message);
            })
            // console.log(tree)
        },
        search: function () {
            //获取树对象，根据id获取
            let treeobj = $.fn.zTree.getZTreeObj("pullDownTreeone");
            //搜索前把所有样式清除
            //方法一：
            // this.init();
            //方法二
            let arrayNodes = treeobj.transformToArray(treeobj.getNodes());
            for (let i = 0; i <arrayNodes.length ; i++) {
                arrayNodes[i].highLight=false;
                //更新节点
                treeobj.updateNode(arrayNodes[i]);
            }
            //第一个参数是在节点的那个属性进行搜索，
            //第二参数：输入搜索的数据
            //第三参数：在那个父节点里面搜索
            if (this.name==''){
                //空的处理
            }else {
                let nodeFuzzy = treeobj.getNodesByParamFuzzy("name", this.name, null);
                for (let i = 0; i < nodeFuzzy.length; i++) {
                    nodeFuzzy[i].highLight = true;
                    //更新节点
                    treeobj.updateNode(nodeFuzzy[i]);

                }
            }
        },
        fontCss: function (treeID, treeNode) {
            return treeNode.highLight ? {'color': 'red'} : {'color': 'black'};
        },
        onClick: function (event, pullDownTreeone, TreeNode) {
            //把公司名回显
            /*this.condition={};
            this.selectPage();*/
            this.name=TreeNode.name;
            //根据公司把公司id设置为当前节点的查询条件
            this.condition.oid  = TreeNode.id;
            // console.log(this.condition.oid)
        },

        selectAll:function () {
            //清空查询条件
            this.condition={};
            this.selectPage();
        },
        toUpdate:function (role) {
            layer.obj = role;
            layer.open({
                type: 2,
                title: '更新',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area : ['70%' , '70%'],
                content: 'role/update.html',
                end:()=>{
                    this.selectAll();
                    this.init();
                }
            });
        },
        toAdd:function(){
            layer.open({
                type: 2,
                title: '工单详情',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area : ['70%' , '70%'],
                content: 'area/add.html',
                end:()=>{
                    this.selectAll();
                    this.init();
                }
            });
        },
        deleteById:function (id) {
            layer.confirm('确认是否要删除？', {
                btn: ['是', '否'] //按钮
            }, () => {
                axios.put(`/guguanjia/manager/role/deleteById/${id}`).then(
                    (result) => {
                        if (result.data.success) {
                            layer.msg(result.data.msg);
                            this.selectAll();
                        } else {
                            layer.msg("删除失败");
                        }
                    }
                ).catch((error) => {
                    layer.msg("删除失败");
                })
            }, function () {
                layer.msg("取消")
            });
        },
        todetil:function (role) {
            layer.obj = role;
            layer.open({
                type: 2,
                title: '详情',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area : ['60%' , '60%'],
                content: 'role/role-detail.html',
            });
        },
        toRoleUser:function (id,rid) {
            layer.obj={
                officeId:id,
                id:rid
            };
            layer.open({
                type: 2,
                title: '角色分配',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area : ['70%' , '70%'],
                content: 'role/role-user.html',
                end:()=>{
                    this.selectAll();
                    this.init();
                }
            });
        },
    },
    created:function () {
        this.selectPage();
        this.selectAllRole();
        //console.log(this.page)
    },
    mounted:function () {//在ul节点挂载后初始化树
        this.init();
    }
});